package org.xtb.taxCalculator.config;

import javax.xml.ws.Endpoint;

import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.xtb.taxCalculator.service.TaxCalculatorService;

@Configuration
public class WebServiceConfig {
    @Autowired
    private TaxCalculatorService taxCalculatorService;

    @Bean(name = "cxfServlet")  // 注入servlet bean name不能dispatcherServlet ,否则会覆盖dispatcherServlet
    public ServletRegistrationBean<CXFServlet> cxfServlet() {
        return new ServletRegistrationBean<>(new CXFServlet(), "/webservice/*");
    }


    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }


    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), taxCalculatorService);
        System.out.println("---------------------------接口发布开始---------------------------------------");
        endpoint.publish("/taxCalculatorService");
        System.out.println("---------------------------接口发布成功---------------------------------------");
        return endpoint;
    }
}